package com.app.micaihu.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.bean.comment.NewsComment;
import com.app.micaihu.listener.CommentListener;
import com.app.micaihu.utils.CommentUtils;
import com.app.micaihu.view.user.userinfo.activity.MyHomepageDynamicActivity;
import com.app.utils.util.view.expression.listener.Clickable;
import com.app.utils.util.view.expression.view.BiaoQinTextView;
import com.blankj.utilcode.util.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 新闻子评论适配器
 * Created by linkai on 2016/7/21.
 */
public class CommentChildAdapter extends AppBaseAdapter<NewsComment> implements View.OnClickListener, Clickable.ClickListener {

    private CommentListener commentListener;//评论监听

    public CommentChildAdapter(List<NewsComment> list, Activity activity) {
        super(list, activity);
    }


    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.item_news_child_comment, null);
            viewHolder = new ViewHolder();
            viewHolder.parentLayout = convertView.findViewById(R.id.comment_parentlayout);
            viewHolder.commentTime = (TextView) convertView.findViewById(R.id.comment_time);
            viewHolder.message = (BiaoQinTextView) convertView.findViewById(R.id.comment_message);
            viewHolder.parentLayout.setOnClickListener(this);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        NewsComment newsComment = list.get(position);
        if (newsComment == null) {
            convertView.setVisibility(View.GONE);
            return convertView;
        }
        if (convertView.getVisibility() == View.GONE) {
            convertView.setVisibility(View.VISIBLE);
        }
        //如果评论不显示则 移除该数据
        if (newsComment.isHide) {
            list.remove(position);
            notifyDataSetChanged();
            if (position >= 0 && position < list.size()) {
                newsComment = list.get(position);
            }
        }
        viewHolder.parentLayout.setTag(R.id.tag_first, newsComment);//设置数据标识
        viewHolder.commentTime.setText(newsComment.getPublishTime());
        ArrayList<String> strs = new ArrayList<>();
        ArrayList<Integer> color = new ArrayList<>();
        ArrayList<Object> parms = new ArrayList<>();
        strs.add(newsComment.getNickName() + ": ");
        color.add(Color.parseColor("#546a93"));
        parms.add(newsComment.getUid());
        String type = newsComment.getReplyType();
        if (!TextUtils.isEmpty(type)) {
            strs.add(type + newsComment.getpNickName() + " ");
            color.add(Color.parseColor("#546a93"));
            parms.add(newsComment.getpUid());
        }
        strs.add(newsComment.getContent());
        color.add(Color.parseColor("#ff333333"));
        parms.add(newsComment);
        viewHolder.message.setText(strs, color, parms, this);
        return convertView;
    }

    /*************** 事件 start *************/
    @Override
    public void onClick(View view) {
        if (view == null) {
            return;
        }
        switch (view.getId()) {
            case R.id.comment_parentlayout://点击父布局弹出评论框
                NewsComment newsComment = null;
                try {
                    newsComment = (NewsComment) view.getTag(R.id.tag_first);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                replyComment(newsComment);
                break;

            default:
                break;
        }
    }


    /**
     * 回复评论
     */
    private void replyComment(NewsComment currentReplyComment) {
        if (currentReplyComment == null) {
            return;
        }

        if (commentListener == null) {
            commentListener = new CommentListener() {
                @Override
                public void commentStart() {

                }

                @Override
                public void commentFail(VolleyError volley) {

                }

                @Override
                public void commentSuccess(NewsComment newsComment) {
                    if (newsComment == null) {
                        return;
                    }
                    list.add(0, newsComment);
                    notifyDataSetChanged();
                }
            };
        }

        CommentUtils.getInstance().showDialog((Activity) context, null, currentReplyComment.getArticleId(), currentReplyComment, commentListener);
    }


    @Override
    public void click(int position, ArrayList<Object> parms) {

        if (parms == null) {
            return;
        }

        if (parms.size() == 2) {
            if (position == 0) {
                String uid = parms.get(0).toString();
                //点击评论用户昵称
                MyHomepageDynamicActivity.goToUserHomePage(ActivityUtils.getTopActivity(), uid);
            } else {
                NewsComment newsComment = (NewsComment) parms.get(1);
                replyComment(newsComment);
            }
        } else if (parms.size() == 3) {
            if (position == 0) {
                String uid = parms.get(0).toString();
                //点击评论用户昵称
                MyHomepageDynamicActivity.goToUserHomePage(ActivityUtils.getTopActivity(), uid);
            } else if (position == 1) {
                String uid = parms.get(1).toString();
                //点击评论用户昵称
                MyHomepageDynamicActivity.goToUserHomePage(ActivityUtils.getTopActivity(), uid);
            } else {
                NewsComment newsComment = (NewsComment) parms.get(2);
                replyComment(newsComment);
            }
        }

    }


    private class ViewHolder {

        TextView commentTime;//发布时间
        BiaoQinTextView message;//评论内容
        View parentLayout;//父布局

    }

}

